package jp.co.lkg.springsmp.controllers;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import jp.co.lkg.springsmp.common.ControllerConstants;
import jp.co.lkg.springsmp.common.Page;
import jp.co.lkg.springsmp.common.db.dto.FlowerinfoDto;
import jp.co.lkg.springsmp.common.db.dto.FlowertypeinfoDto;
import jp.co.lkg.springsmp.common.db.dto.GoodinfoDto;
import jp.co.lkg.springsmp.common.db.dto.OrderinfoDto;
import jp.co.lkg.springsmp.common.db.dto.PrivateinfoDto;
import jp.co.lkg.springsmp.common.db.dto.UserinfoDto;
import jp.co.lkg.springsmp.controllers.form.AdminForm;
import jp.co.lkg.springsmp.services.FlowerinfoService;
import jp.co.lkg.springsmp.services.FlowertypeinfoService;
import jp.co.lkg.springsmp.services.GoodinfoService;
import jp.co.lkg.springsmp.services.OrderinfoService;
import jp.co.lkg.springsmp.services.PrivateinfoService;
import jp.co.lkg.springsmp.services.UserinfoService;

@RequestMapping("/admin")
@Controller
public class AdminController {

	protected final Log logger = LogFactory.getLog(getClass());

	@Autowired
	private GoodinfoService goodinfoService;

	@Autowired
	private FlowerinfoService flowerinfoService;
	
	@Autowired
	private FlowertypeinfoService flowertypeinfoService ;
	
	@Autowired
	private OrderinfoService orderinfoService;
	
	@Autowired
	private PrivateinfoService privateinfoService;
	
	@Autowired
	private UserinfoService userinfoService;

	/**********************************************************************************************************************
	 * 程序启动后的首页跳转
	 */
	@RequestMapping("/main")
	public ModelAndView main(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_ADMIN_FORM) final AdminForm adminForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
		ModelAndView modelAndView = new ModelAndView("admin");
		return modelAndView;
	}

	/**********************************************************************************************************************
	 * 商品列表界面
	 */
	@RequestMapping("/test")
	public String showjsp(Model model, Integer currPage, GoodinfoDto dto,HttpServletRequest request) {
		
		GoodinfoDto gdto = new GoodinfoDto();
		String flowerTypeName= request.getParameter("flowerTypeName");
		//当不选择type查询的时候，将flowerTypeName默认成是0,然后在service层中进行处理
		if (flowerTypeName==null) {
			flowerTypeName="0";
		}
		gdto.setGoodType(flowerTypeName);
		request.setAttribute("flowerTypeName", flowerTypeName);
		
		Page pagelist = goodinfoService.selectPage(currPage, gdto);
		
		FlowertypeinfoDto dto1= new FlowertypeinfoDto();
		List<FlowertypeinfoDto> flowertypelist=flowertypeinfoService.select(dto1);
		model.addAttribute("flowertypelist",flowertypelist);
		
		model.addAttribute("pagelist", pagelist);

		return "goodlist";

	}
	
	/**********************************************************************************************************************
	 * 私人定制鲜花列表
	 */
	@RequestMapping("/flowerlist")
	public String showflower(Model model, Integer currPage, GoodinfoDto dto,HttpServletRequest request) {
		
	
		FlowerinfoDto flowerinfoDto=new FlowerinfoDto();
		Page pagelist=flowerinfoService.selectPage(currPage, flowerinfoDto);
		model.addAttribute("pagelist", pagelist);

		return "flowerlist";

	}
	
	/**********************************************************************************************************************
	 * 用户列表
	 */
	@RequestMapping("/userlist")
	public String userlist(Model model, Integer currPage,HttpServletRequest request) {
		UserinfoDto userinfoDto=new UserinfoDto();
		Page pagelist=userinfoService.selectPage(currPage, userinfoDto);
		model.addAttribute("pagelist", pagelist);

		return "userlist";

	}
	/**********************************************************************************************************************
	 * 去新增商品界面
	 */
	@RequestMapping("/toaddgood")
	public ModelAndView toaddgood(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_ADMIN_FORM) final AdminForm adminForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
		
		FlowertypeinfoDto dto= new FlowertypeinfoDto();
		List<FlowertypeinfoDto> flowertypelist=flowertypeinfoService.select(dto);
		adminForm.setFlowertypelist(flowertypelist);
		
		model.addAttribute(ControllerConstants.AttributeKeys.KEY_ADMIN_FORM, adminForm);
		ModelAndView modelAndView = new ModelAndView("goodadd");
		return modelAndView;
	}
	/**********************************************************************************************************************
	 * 新增商品
	 */
	@RequestMapping("/addgood")
	public ModelAndView addgood(@RequestParam(value = "file") MultipartFile file,
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_ADMIN_FORM) final AdminForm adminForm,
			HttpServletRequest request, HttpServletResponse response, final Model model)
			throws IllegalStateException, IOException {

		String basePath = "D:\\sss\\last\\Flower\\WebContent\\resources\\upload\\good\\";

		// 获取文件名字
		String fileName = file.getOriginalFilename();
		// 定义一个路径
		// String newPath ="D:\\upload\\";
		// 拼接成file类型
		File newFile = new java.io.File(basePath + fileName);
		// 写入文件
		file.transferTo(newFile);
		// 写入数据库
		String target = fileName;
		GoodinfoDto goodinfoDto = adminForm.getInsertDto();
		goodinfoDto.setGoodImage(target);
		goodinfoService.insert(goodinfoDto);
		
		ModelAndView modelAndView = new ModelAndView("admin");
		return modelAndView;
	}
	/**********************************************************************************************************************
	 * 去往新增鲜花种类的界面
	 */
	@RequestMapping("/toaddflower")
	public ModelAndView toaddflower(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_ADMIN_FORM) final AdminForm adminForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
		ModelAndView modelAndView = new ModelAndView("floweradd");
		return modelAndView;
	}
	/**********************************************************************************************************************
	 * 新增鲜花
	 */
	@RequestMapping("/addflower")
	public ModelAndView addflower(@RequestParam(value = "file") MultipartFile file,
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_ADMIN_FORM) final AdminForm adminForm,
			HttpServletRequest request, HttpServletResponse response, final Model model)
			throws IllegalStateException, IOException {

		String basePath = "D:\\sss\\last\\Flower\\WebContent\\resources\\upload\\flower\\";

		// 获取文件名字
		String fileName = file.getOriginalFilename();
		// 定义一个路径
		// String newPath ="D:\\upload\\";
		// 拼接成file类型
		File newFile = new java.io.File(basePath + fileName);
		// 写入文件
		file.transferTo(newFile);
		// 写入数据库
		String target = fileName;
		FlowerinfoDto flowerinfoDto = adminForm.getFlowerinsertDto();
		flowerinfoDto.setFlowerImage(target);

		flowerinfoService.insert(flowerinfoDto);
		ModelAndView modelAndView = new ModelAndView("admin");
		return modelAndView;
	}
	/**********************************************************************************************************************
	 * 去往商品更新界面
	 */
	@RequestMapping("/toupdate")
	public String update1(GoodinfoDto dto, Model model) {
		List<GoodinfoDto> goodinfoDto = goodinfoService.select(dto);
		model.addAttribute("list", goodinfoDto);
		return "goodupdate";

	}
	/**********************************************************************************************************************
	 * 去往鲜花更新界面
	 */
	@RequestMapping("/toflowerupdate")
	public String updateflower(FlowerinfoDto dto, Model model) {
		List<FlowerinfoDto> flowerinfolist = flowerinfoService.select(dto);
		model.addAttribute("list", flowerinfolist);
		return "flowerupdate";

	}
	
	/**********************************************************************************************************************
	 * 商品更新
	 */
	@RequestMapping("/goodupdate")
    public ModelAndView goodupdate(@RequestParam(value = "file") MultipartFile file,
    		//　Data：　JSP　→　Form　
    		@ModelAttribute(ControllerConstants.AttributeKeys.KEY_ADMIN_FORM) final AdminForm adminForm, 
    		HttpServletRequest request,
    		HttpServletResponse response,
    		final Model model) throws IllegalStateException, IOException
            {
				GoodinfoDto dto=adminForm.getUpdateDto();
				//这边来判断图片是否发生改变
				if (!file.isEmpty()) {
				
					// 定义一个路径
					String basePath = "D:\\sss\\last\\Flower\\WebContent\\resources\\upload\\good\\";
					String fileName = file.getOriginalFilename();
					// 拼接成file类型
					File newFile = new java.io.File(basePath + fileName);
					// 写入文件
					file.transferTo(newFile);
					// 写入数据库
					String target = fileName;
					dto.setGoodImage(target);
					
			    	if (adminForm.getUpdateDto().getGoodName().equals(" *")
				    			&& adminForm.getUpdateDto().getGoodAddtime().equals(" *") 
				    			&& adminForm.getUpdateDto().getGoodAddadmin().equals(" *")
				    			&& adminForm.getUpdateDto().getGoodImage().equals(" *")
				    			&& adminForm.getUpdateDto().getGoodPrice().equals(" *")
				    		&& adminForm.getUpdateDto().getGoodIntroduction().equals(" *")) {
							
							goodinfoService.updateIncludeNull(dto);
						}else {
							goodinfoService.updateByPk(dto);
						}
				}else {
					
			    	if (adminForm.getUpdateDto().getGoodName().equals(" *")
			    			&& adminForm.getUpdateDto().getGoodAddtime().equals(" *") 
			    			&& adminForm.getUpdateDto().getGoodAddadmin().equals(" *")
			    			&& adminForm.getUpdateDto().getGoodImage().equals(" *")
			    			&& adminForm.getUpdateDto().getGoodPrice().equals(" *")
			    			&& adminForm.getUpdateDto().getGoodIntroduction().equals(" *")) {
						
						goodinfoService.updateIncludeNull(adminForm.getUpdateDto());
					}else {
						goodinfoService.updateByPk(adminForm.getUpdateDto());
					}
				}
				
				
			
			
			return new ModelAndView("admin");
    	
    }
	/**********************************************************************************************************************
	 * 鲜花更新
	 */
	@RequestMapping("/flowerupdate")
    public ModelAndView flowerupdate(@RequestParam(value = "file") MultipartFile file,
    		//　Data：　JSP　→　Form　
    		@ModelAttribute(ControllerConstants.AttributeKeys.KEY_ADMIN_FORM) final AdminForm adminForm, 
    		HttpServletRequest request,
    		HttpServletResponse response,
    		final Model model) throws IllegalStateException, IOException
            {
				FlowerinfoDto dto=adminForm.getFlowerupdateDto();
				//这边来判断图片是否发生改变
				if (!file.isEmpty()) {
				
					// 定义一个路径
					String basePath = "D:\\sss\\last\\Flower\\WebContent\\resources\\upload\\flower\\";
					String fileName = file.getOriginalFilename();
					// 拼接成file类型
					File newFile = new java.io.File(basePath + fileName);
					// 写入文件
					file.transferTo(newFile);
					// 写入数据库
					String target = fileName;
					dto.setFlowerImage(target);
					
			    	if (adminForm.getFlowerupdateDto().getFlowerId().equals(" *")
				    			&& adminForm.getFlowerupdateDto().getFlowerName().equals(" *")
				    			&& adminForm.getFlowerupdateDto().getFlowerPrice().equals(" *")
				    			&& adminForm.getFlowerupdateDto().getFlowerImage().equals(" *")
				    			&& adminForm.getFlowerupdateDto().getFlowerDetails().equals(" *")
				    			&& adminForm.getFlowerupdateDto().getFlowerAddtime().equals(" *")
				    			&& adminForm.getFlowerupdateDto().getFlowerAddadmin().equals(" *")) 
			    	{
							flowerinfoService.updateIncludeNull(dto);
						}else {
							flowerinfoService.updateByPk(dto);
						}
				}else {
					
					if (adminForm.getFlowerupdateDto().getFlowerId().equals(" *")
			    			&& adminForm.getFlowerupdateDto().getFlowerName().equals(" *")
			    			&& adminForm.getFlowerupdateDto().getFlowerPrice().equals(" *")
			    			&& adminForm.getFlowerupdateDto().getFlowerImage().equals(" *")
			    			&& adminForm.getFlowerupdateDto().getFlowerDetails().equals(" *")
			    			&& adminForm.getFlowerupdateDto().getFlowerAddtime().equals(" *")
			    			&& adminForm.getFlowerupdateDto().getFlowerAddadmin().equals(" *")) 
		    	{
						flowerinfoService.updateIncludeNull(adminForm.getFlowerupdateDto());
					}else {
						flowerinfoService.updateByPk(adminForm.getFlowerupdateDto());
					}
				}
				
				
			
			
			return new ModelAndView("admin");
    	
    }
	/**********************************************************************************************************************
	 * 删除商品
	 */
	@RequestMapping("/delgood")
	public String delgood(GoodinfoDto dto, Integer currPage, Model model,HttpServletRequest request) {
		goodinfoService.deleteByPk(dto);
		GoodinfoDto gdto = new GoodinfoDto();
		String flowerTypeName= request.getParameter("flowerTypeName");
		//当不选择type查询的时候，将flowerTypeName默认成是0,然后在service层中进行处理
		if (flowerTypeName==null) {
			flowerTypeName="0";
		}
		gdto.setGoodType(flowerTypeName);
		request.setAttribute("flowerTypeName", flowerTypeName);
		
		Page pagelist = goodinfoService.selectPage(currPage, gdto);
		
		FlowertypeinfoDto dto1= new FlowertypeinfoDto();
		List<FlowertypeinfoDto> flowertypelist=flowertypeinfoService.select(dto1);
		model.addAttribute("flowertypelist",flowertypelist);
		model.addAttribute("pagelist", pagelist);
		return "goodlist";

	}

	/**********************************************************************************************************************
	 * 删除鲜花
	 */
	@RequestMapping("/delflower")
	public String delflower(FlowerinfoDto dto, Integer currPage, Model model) {
		flowerinfoService.deleteByPk(dto);
		FlowerinfoDto fdto=new FlowerinfoDto();

		Page pagelist = flowerinfoService.selectPage(currPage, fdto);
		model.addAttribute("pagelist", pagelist);
		return "flowerlist";

	}
	
	/**********************************************************************************************************************
	 * 订单列表
	 */
	@RequestMapping("/orderlist")
	public String oredrlist(Model model, Integer currPage, OrderinfoDto dto,HttpServletRequest request) {
		OrderinfoDto orderinfoDto=new OrderinfoDto();
		Page pagelist=orderinfoService.selectPage(currPage, orderinfoDto);
		model.addAttribute("pagelist", pagelist);
	
		return "orderlist";

	}
	
	/**********************************************************************************************************************
	 * 删除订单
	 */
	@RequestMapping("/delorder")
	public String delorder(OrderinfoDto dto, Integer currPage, Model model) {
		orderinfoService.deleteByPk(dto);
		OrderinfoDto odto=new OrderinfoDto();
		

		Page pagelist = orderinfoService.selectPage(currPage, odto);
		model.addAttribute("pagelist", pagelist);
		return "orderlist";

	}
	
	/**********************************************************************************************************************
	 * 去往订单更新界面
	 */
	@RequestMapping("/toorderupdate")
	public String toorderupdate(OrderinfoDto dto, Model model) {
		List<OrderinfoDto> orderinfolist = orderinfoService.select(dto);
		model.addAttribute("list", orderinfolist);
		return "orderupdate";

	}
	
	/**********************************************************************************************************************
	 * 訂單更新
	 */
	@RequestMapping("/orderupdate")
    public ModelAndView orderupdate(
    		@ModelAttribute(ControllerConstants.AttributeKeys.KEY_ADMIN_FORM) final AdminForm adminForm, 
    		HttpServletRequest request,
    		HttpServletResponse response,
    		final Model model) throws IllegalStateException, IOException
            {
				OrderinfoDto dto=adminForm.getOrderupdateDto();
				//这边来判断图片是否发生改变
				
					
			    	if (adminForm.getOrderupdateDto().getOrderId().equals(" *")
				    			&& adminForm.getOrderupdateDto().getOrderName().equals(" *") 
				    			&& adminForm.getOrderupdateDto().getOrderCost().equals(" *")
				    			&& adminForm.getOrderupdateDto().getOrderPaystatus().equals(" *")
				    			&& adminForm.getOrderupdateDto().getOrderTime().equals(" *")
				    			&& adminForm.getOrderupdateDto().getOrderImage().equals(" *")
				    			&& adminForm.getOrderupdateDto().getOrderStatus().equals(" *")
				    			&& adminForm.getOrderupdateDto().getOrderOwner().equals(" *")
				    			) 
			    	{
			    			orderinfoService.updateIncludeNull(dto);
						}else {
							orderinfoService.updateByPk(dto);
						}
			
			    	return new ModelAndView("admin");
    	
    }
	
	/**********************************************************************************************************************
	 * 私人定制列表
	 */
	@RequestMapping("/privateorderlist")
	public String privateorderlist(Model model, Integer currPage, PrivateinfoDto dto,HttpServletRequest request) {
		PrivateinfoDto privateinfoDto=new PrivateinfoDto();
		
		Page pagelist=privateinfoService.selectPage(currPage, privateinfoDto);
		model.addAttribute("pagelist", pagelist);
	
		return "privateorderlist";

	}
	
	/**********************************************************************************************************************
	 * 删除私人订单
	 */
	@RequestMapping("/delprivate")
	public String delprivate(PrivateinfoDto dto, Integer currPage, Model model) {
		privateinfoService.deleteByPk(dto);
		PrivateinfoDto pdto=new PrivateinfoDto();
		Page pagelist = privateinfoService.selectPage(currPage, pdto);
		model.addAttribute("pagelist", pagelist);
		return "privateorderlist";

	}
	
	/**********************************************************************************************************************
	 * 去往私人订单订单更新界面
	 */
	@RequestMapping("/toprivateupdate")
	public String toprivateupdate(PrivateinfoDto dto, Model model) {
		List<PrivateinfoDto> privatelist = privateinfoService.select(dto);
		model.addAttribute("list", privatelist);
		return "privateupdate";

	}
	
	/**********************************************************************************************************************
	 * 私人定製更新訂單
	 */
	@RequestMapping("/privateupdate")
    public ModelAndView privateupdate(
    		@ModelAttribute(ControllerConstants.AttributeKeys.KEY_ADMIN_FORM) final AdminForm adminForm, 
    		HttpServletRequest request,
    		HttpServletResponse response,
    		final Model model) throws IllegalStateException, IOException
            {
				PrivateinfoDto dto=adminForm.getPrivateupdateDto();
				//这边来判断图片是否发生改变
				
					
			    	if (adminForm.getPrivateupdateDto().getPrivateId().equals(" *")
				    			&& adminForm.getPrivateupdateDto().getPrivateName().equals(" *") 
				    			&& adminForm.getPrivateupdateDto().getPrivateCost().equals(" *")
				    			&& adminForm.getPrivateupdateDto().getPrivatePaystatus().equals(" *")
				    			&& adminForm.getPrivateupdateDto().getPrivateTime().equals(" *")
				    			&& adminForm.getPrivateupdateDto().getPrivateDetails().equals(" *")
				    			&& adminForm.getPrivateupdateDto().getPrivateStatus().equals(" *")
				    			&& adminForm.getPrivateupdateDto().getPrivateOwner().equals(" *")
				    			&& adminForm.getPrivateupdateDto().getPrivateIntroduction().equals(" *")
				    			) 
			    	{
			    			privateinfoService.updateIncludeNull(dto);
						}else {
							privateinfoService.updateByPk(dto);
						}
			
			    	return new ModelAndView("admin");
    	
    }
	
}